package com.sskj.level.ui.activity;


import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.level.R;
import com.sskj.level.R2;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.TabItem;
import com.sskj.level.bean.TradeInfoBean;
import com.sskj.level.presenter.TradeActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.LEVEL_TRADE)//交易单界面
public class TradeActivity extends BaseActivity<TradeActivityPresenter> {
    @BindView(R2.id.slidingTabLayout)
    CommonTabLayout slidingTabLayout;
    ArrayList<Fragment> mFragments;
    private Disposable subscribe;
    @BindView(R2.id.level_all_pc)
    TextView levelAllPc;
    @Autowired
    public String code;

    @Override
    protected int getLayoutResId() {
        return R.layout.level_activity_trade;
    }

    @Override
    public TradeActivityPresenter getPresenter() {
        return new TradeActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.levelTradeActivity0));
        ARouter.getInstance().inject(this);
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.levelTradeActivity1), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.levelTradeActivity2), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.levelTradeActivity3), 0, 0));
        mFragments = new ArrayList<>();
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.LEVEL_FRAGMENT_HOLDER).navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.LEVEL_FRAGMENT_ENTRUST).navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.LEVEL_FRAGMENT_DEAL).navigation());
        slidingTabLayout.setTabData(tabItems, this, R.id.tradeFrameLayout, mFragments);
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (0 == position) {
                    levelAllPc.setVisibility(View.VISIBLE);
                } else {
                    levelAllPc.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        ClickUtil.click(levelAllPc, () -> {
            mPresenter.levelAllPc(SPUtil.get(SPConfig.ID, ""), code);
        });
        startTimer();
    }

    private void startTimer() {
        DisposUtil.close(subscribe);
        subscribe = Flowable.interval(0, 5, TimeUnit.SECONDS)
                .onBackpressureDrop()
                .subscribe(i -> mPresenter.getRisk());
    }

    private void setItem(int itemId, String title, String content) {
        setItem(itemId, title, content, false);
    }

    private void setItem(int itemId, String title, String content, boolean isRed) {
        ((TextView) findViewById(itemId).findViewById(R.id.tv_title)).setText(title);
        ((TextView) findViewById(itemId).findViewById(R.id.tv_content)).setText(content);
        if (isRed) {
            ((TextView) findViewById(itemId).findViewById(R.id.tv_title)).setTextColor(ContextCompat.getColor(this, R.color.level_FF3F3E));
            ((TextView) findViewById(itemId).findViewById(R.id.tv_content)).setTextColor(ContextCompat.getColor(this, R.color.level_FF3F3E));
            ((LinearLayout) findViewById(itemId)).setBackgroundColor(Color.WHITE);
        }
    }

    public void updateTrade(TradeInfoBean bean) {
        setItem(R.id.trade_item_dtqy, App.INSTANCE.getString(R.string.levelTradeActivity4), NumberUtil.keepMax4(bean.getAllFound() + ""));
        setItem(R.id.trade_item_kyzj, App.INSTANCE.getString(R.string.levelTradeActivity6), NumberUtil.keepMax4(bean.getUsableFund() + ""));
        setItem(R.id.trade_item_djbzj, App.INSTANCE.getString(R.string.levelTradeActivity8), NumberUtil.keepMax4(bean.getHasUsedFund() + ""));
        setItem(R.id.trade_item_fdyk, App.INSTANCE.getString(R.string.levelTradeActivity10), NumberUtil.keepMax4(bean.getFloatWinLoseFee() + ""));
//        setItem(R.id.trade_item_pcyk, App.INSTANCE.getString(R.string.levelTradeActivity12), NumberUtil.keepMax4(bean.getCloseWinLoseFee() + ""));
        setItem(R.id.trade_item_fxl, "爆仓率", bean.getBlowingUprate() + "%", true);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(subscribe);
        super.onDestroy();
    }
}
